﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class UserControls_EarningsGrowth : System.Web.UI.UserControl
{

  private IndexNumbers indexNumbers;

  protected void Page_Load(object sender, EventArgs e)
  {
    string id = Request.Params["id"];
    StockAnalysis analysis = new StockAnalysis();
    DataTable dt = analysis.GetEarningGrowthTable(id);
    gridviewEarningsGrowth.DataSource = ConvertSubFigures.Convert(ConvertToIndexNumbers(dt));
    gridviewEarningsGrowth.DataBind();

    Graph g = new Graph();
    Dictionary<string, string[]> graphData = ConvertGraphData.Convert(dt);

    string[] year;
    string[] grossMarginRatio;
    string[] operationMargin;
    string[] revenue;
    string[] grossMargin;
    string[] result;

    graphData.TryGetValue("year", out year);

    graphData.TryGetValue("gross_margin_ratio", out grossMarginRatio);
    graphGrossMarginRatio.InnerHtml = g.drawGraphOneAttributeWithFiveFigures(Dictionary.Translate("gross_margin_ratio"), "XAxis", Abbreviations.getAbbreviations("gross_margin_ratio"), grossMarginRatio, year, false, true, false);

    graphData.TryGetValue("operating_margin", out operationMargin);
    graphOperatingMargin.InnerHtml = g.drawGraphOneAttributeWithFiveFigures(Dictionary.Translate("operating_margin"), "XAxis", Abbreviations.getAbbreviations("operating_margin"), operationMargin, year, false, true, false);

    graphData.TryGetValue("revenue", out revenue);
    graphData.TryGetValue("gross_margin", out grossMargin);
    graphData.TryGetValue("result", out result);
    graphRevenueGrossmarginResult.InnerHtml = g.drawGraphThreeAttributeWithFiveFigures("Indekstal", "XAxis", Abbreviations.getAbbreviations("revenue"), Abbreviations.getAbbreviations("gross_margin_ratio"), Abbreviations.getAbbreviations("result"), ToIndexNumber(revenue), ToIndexNumber(grossMargin), ToIndexNumber(result), year, false, false);

  }

  private DataTable ConvertToIndexNumbers(DataTable dt)
  {
    string[] revenue = new string[5];
    string[] grossMargin = new string[5];
    string[] result = new string[5];

    for (int i = 0; i < dt.Rows.Count; i++)
    {
      revenue[i] = dt.Rows[i][3].ToString();
      grossMargin[i] = dt.Rows[i][4].ToString();
      result[i] = dt.Rows[i][5].ToString();
    }

    revenue = ToIndexNumber(revenue);
    grossMargin = ToIndexNumber(grossMargin);
    result = ToIndexNumber(result);

    for (int i = 0; i < dt.Rows.Count; i++)
    {
      dt.Rows[i][3] = revenue[i];
      dt.Rows[i][4] = grossMargin[i];
      dt.Rows[i][5] = result[i];
    }
    return dt;
  }


  private string[] ToIndexNumber(string[] numbers)
  {
    int[] index = new int[6];

    for (int i = 0; i < numbers.Length; i++)
      index[i] = int.Parse(numbers[i]);

    if (numbers.Length == 5)
      index[5] = 0;

    indexNumbers = new IndexNumbers(index[0], index[0], index[1], index[2], index[3], index[4], index[5]);

    for (int i = 0; i < numbers.Length; i++)
      numbers[i] = indexNumbers.indexNumbers[i].ToString();

    return numbers;
  }
}